/**
 * select控件
 * @authors supmain (mtingfeng@gmail.com)
 * @date    2014-09-04 11:23:07
 * @update  2014-09-04 11:23:07
 * @for webapp.html
 * @version $Id$
 */
(function($) {
    $.fn.extend({
        dropBox:function(options){
          var $this = $(this),
              options = options || {};
          var common = {
            /*判断是否select标签*/
            isSelectTag: function(argument) {
              var bool = true;
              $this.each(function(index, val) {
                var tag = $(this).get(0).tagName.toLowerCase();
                //console.log('tag:'+tag);
                if(tag == 'select'){
                  return true;
                }else{
                  bool = false;
                  return false;
                }
              });
              return bool;
            }
          };
          //如果不是select元素则返回
          if(!common.isSelectTag()){return $this;}
          //初始化每一个select
          $this.each(function(index, val) {
            /*初始化参数*/
            var _this = $(this);
            var width = options.width ? options.width : _this.outerWidth(),
              height = options.height ? options.height : _this.outerHeight(),
              name = options.name ? options.name : 'selectname',
              size = options.size ? options.size : '5',
              sltheight = options.sltheight ? options.sltheight : 100;
            //disabled = options.disabled ? options.disabled : '20';
            //console.log('width:'+width+';height:'+height+';name:'+name+';size:'+size+';');
            var $parent = _this.parent();
            var $prev = _this.prev();
            var dropbox_html = '';
            var dropbox_options = (function() {
              var arr = [];
              $.each(_this.children('option'),function(index, val) {
                 var prop = {};
                 prop.value = $(this).attr('value');
                 prop.text = $(this).text();
                 arr.push(prop);
              });
              return arr;
            })();
            var dropbox_title_value = dropbox_options[0].value;
            var dropbox_title_text = dropbox_options[0].text;
            //console.log(dropbox_options);
            var dropbox_options_html = '';
            $.each(dropbox_options, function(index, val) {
               dropbox_options_html = dropbox_options_html + '<li data-value="'+val.value+'">'+val.text+'</li>';
            });
            dropbox_html = '<div class="dropbox"><p class="dropbox_select"><span class="dropbox_value" data-value="'+dropbox_title_value+'">'+dropbox_title_text+'</span></p><div class="dropbox_options"><ul>'+dropbox_options_html+'</ul></div></div>';
            /*替换成dropbox控件*/
            _this.remove();
            var $dropbox_html = $(dropbox_html);
            if($prev.length == 0){
              $parent.prepend($dropbox_html);
            }else{
              $prev.after($dropbox_html);
            }
            /*设置事件，添加事件*/
            var $dropbox_select = $dropbox_html.find('.dropbox_select'),
                $dropbox_options = $dropbox_html.find('.dropbox_options');
            $dropbox_html.css({
              width: width,
              name: name
            });
            $dropbox_select.css({
              width: width,
              height: height
            });
            $dropbox_options.css({
              position: 'absolute',
              top: $dropbox_html.outerHeight()-1,
              left: 0,
              width: width,
              display: 'none',
              'z-index': 999
            });
            if ($dropbox_options.height() >= sltheight ) {
              $dropbox_options.css({
                'overflow-y': 'scroll',
                'height': sltheight
              });
            };
            //console.log($dropbox_select.length)
            //alert(($dropbox_html.outerHeight()-2)+'px')
            //解决IE6下top设置的bug
            $dropbox_options.css('top',$dropbox_html.outerHeight()-1)
            $dropbox_select.on('click', function(event) {
              var $options_box = $(this).next('.dropbox_options');
              $('.dropbox_options').hide();
              $options_box.toggle();
              return false;
            });
            $dropbox_options.on('click', 'li' , function(event) {
              var $this = $(this);
              var value = $.trim($this.data('value')),
                  text = $.trim($this.text());
              var $title = $this.parents('.dropbox').find('.dropbox_value');
              var $options_box  = $(this).parents('.dropbox_options');
              $title.attr('data-value', value);
              $title.text(text);
              $options_box.toggle();
              return false;
            }).on('mouseover', 'li', function(event) {
              $(this).siblings('li').removeClass('hover');
              $(this).addClass('hover');
            });
          });
          $(document).on('click', function(event) {
            $('.dropbox_options').hide();
          }).trigger('select_loaded');
          return $this;
        }
    });
})(jQuery);